package com.itheima.servlet; /**
 * @author Xiao
 * @date 2021/9/6
 */

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.City;
import com.itheima.service.ProvinceService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/cityServlet")
public class CityServlet extends HttpServlet {
    ProvinceService provinceService = new ProvinceService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收json请求参数
        String json = request.getReader().readLine();
        //将json字符串转换为Java对象
        City city = JSON.parseObject(json, City.class);
        //查询数据库
        Integer fatherID = city.getFatherID();
        List<City> cities = provinceService.selectCityByFID(fatherID);
        //将Java对象转换为JSON字符串
        String jsonStr = JSON.toJSONString(cities);
        //响应数据
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(jsonStr);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
